package org.example.ttms.controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Text;
import org.example.ttms.customize.BuyTicket;
import org.example.ttms.customize.Window;
import org.example.ttms.entityClass.ticket.Ticket;
import org.example.ttms.entityClass.train.Trips;

import java.util.Optional;

public class BuyTicketController {

    @FXML
    private Text businessText1;

    @FXML
    private Text firstText1;

    @FXML
    private Text secondText1;

    @FXML
    private Text businessText2;

    @FXML
    private Text firstText2;

    @FXML
    private Text secondText2;

    @FXML
    private Button businessButton;

    @FXML
    private Button firsetButton;

    @FXML
    private Button secondButton;

    private Trips trips;

    public Trips getTrips() {
        return trips;
    }

    public void setTrips(Trips trips) {
        this.trips = trips;
    }

    public void initialize(){
        trips = BuyTicket.trips;
        Ticket ticket = new Ticket();
        //Text
        businessText1.setText("票数:"+trips.obtainTicketNum("商务座"));
        businessText2.setText("价格:"+trips.obtainTicketPrice("商务座"));
        firstText1.setText("票数:"+trips.obtainTicketNum("一等座"));
        firstText2.setText("价格:"+trips.obtainTicketPrice("一等座"));
        secondText1.setText("票数:"+trips.obtainTicketNum("二等座"));
        secondText2.setText("价格:"+trips.obtainTicketPrice("二等座"));
        businessButton.setDisable(trips.obtainTicketNum("商务座") == 0);
        firsetButton.setDisable(trips.obtainTicketNum("一等座") == 0);
        secondButton.setDisable(trips.obtainTicketNum("二等座") == 0);
    }
    @FXML
    void chickBuyBusiness(ActionEvent event) {
        buyAlert("商务座");
    }

    @FXML
    void chickBuyFirst(ActionEvent event) {
        buyAlert("一等座");
    }

    @FXML
    void chickBuySecond(ActionEvent event) {
        buyAlert("二等座");
    }
    //判断是否购买框
    public void buyAlert(String type){
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText("购买"+type);
        alert.setTitle("");
        alert.getDialogPane().setStyle("-fx-font-size: 18px;");
        Optional<ButtonType> result = alert.showAndWait();

        // 判断用户选择
        if (result.isPresent() && result.get() == ButtonType.OK) {
            BuyTicket buyTicket = new BuyTicket();
            buyTicket.buyTicket(type, "购票成功");
            Window.stage.close();
        }
    }

}
